package com.yunshipei.ui.fragment;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.yunshipei.common.Globals;
import com.yunshipei.enterplorer.R;

import java.util.HashSet;
import java.util.Set;

import butterknife.Bind;
import butterknife.ButterKnife;

public class CompanyNameHistoryFragment extends Fragment {

    @Bind(R.id.list_company_name_his)
    protected ListView mCompanyNameLv;

    private Context mContext;
    private Set<String> mCompanyNameHisData;
    private SharedPreferences mPreferences;
    private ContentAdapter mContentAdapter;

    private OnFragmentInteraction onFragmentInteraction;

    public static CompanyNameHistoryFragment newInstance() {
        return new CompanyNameHistoryFragment();
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnFragmentInteraction) {
            onFragmentInteraction = (OnFragmentInteraction) context;
        }
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.mContext = getContext();
        mPreferences = mContext.getSharedPreferences(Globals.NO_CLEAR_SHP, Context.MODE_PRIVATE);
        mCompanyNameHisData = mPreferences.getStringSet(Globals.MY_OLD_DOMAIN_NAME_DATAS, new HashSet<String>());
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_company_name_history, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mContentAdapter = new ContentAdapter(mContext, mCompanyNameHisData, new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                int position = (int) v.getTag();
                switch (v.getId()) {
                    case R.id.old_domain:
                        if (onFragmentInteraction != null) {
                            onFragmentInteraction.itemSelected(mContentAdapter.getItem(position));
                        }
                        break;
                    case R.id.old_data_remove:
                        String companyName = mContentAdapter.getItem(position);
                        if (mCompanyNameHisData.contains(companyName)) {
                            mCompanyNameHisData.remove(companyName);
                            mPreferences.edit().putStringSet(Globals.MY_OLD_DOMAIN_NAME_DATAS, mCompanyNameHisData).apply();
                            mContentAdapter.notifyDataSetChanged();
                        }
                        if (onFragmentInteraction != null) {
                            onFragmentInteraction.closeItem(companyName);
                        }
                        break;
                }
            }
        });
        mCompanyNameLv.setAdapter(mContentAdapter);
    }

    public static class ContentAdapter extends BaseAdapter {

        private Set<String> dataSet;
        private Context context;
        private View.OnClickListener clickListener;

        ContentAdapter(Context context, Set<String> dataSet, View.OnClickListener clickListener) {
            this.context = context;
            this.dataSet = dataSet;
            this.clickListener = clickListener;
        }

        @Override
        public int getCount() {
            return dataSet == null ? 0 : dataSet.size();
        }

        @Override
        public String getItem(int position) {
            int index = 0;
            for (String str : dataSet) {
                if (index == position) {
                    return str;
                }
                index++;
            }
            return "";
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder;
            if (convertView == null) {
                convertView = LayoutInflater.from(context).inflate(R.layout.domain_popwindow_item, parent, false);
                viewHolder = new ViewHolder(convertView);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            viewHolder.tvCompanyName.setText(getItem(position));
            viewHolder.tvCompanyName.setOnClickListener(clickListener);
            viewHolder.tvCompanyName.setTag(position);
            viewHolder.ivCloseItem.setTag(position);
            viewHolder.ivCloseItem.setOnClickListener(clickListener);
            return convertView;
        }

        static class ViewHolder {
            @Bind(R.id.old_domain)
            TextView tvCompanyName;
            @Bind(R.id.old_data_remove)
            ImageView ivCloseItem;

            public ViewHolder(View convertView) {
                ButterKnife.bind(this, convertView);
            }
        }
    }

    public interface OnFragmentInteraction {
        void itemSelected(String companyName);

        void closeItem(String companyName);
    }
}
